# Python 2.6.4
# Project Euler, Problem 203
# Copyright 2010 Talha Zaman

from mine import timed, sieveprimes, nCk
from itertools import takewhile

def squarefree(n, psq):
    return all(n%p != 0 for p in takewhile(lambda x: x<=n, psq))

def pascal(numrows):
    distinct = set([1])
    rem = numrows
    prevrow = [1] * numrows
    while rem > 1 + (numrows / 2): 
        nextrow = [1]
        for i in xrange(rem-2):
            nextrow.append(nextrow[-1] + prevrow[i+1])
        prevrow = nextrow
        rem -= 1
        for num in prevrow: distinct.add(num)
    return distinct

@timed
def sqfsum(numrows):
    psq = [p**2 for p in sieveprimes(numrows)]
    return sum(i for i in pascal(numrows) if squarefree(i, psq))
